package com.lbs.web.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lbs.entity.Course;
import com.lbs.service.CourseService;
import com.lbs.service.Impl.CourseServiceImpl;
import com.lbs.utils.BaseServlet;
import com.lbs.utils.FileUploadUtils;
import com.lbs.utils.PageUtils;
import com.lbs.utils.ResultVO;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @author Administrator
 * @date 2021/5/31 19:33
 * @description
 **/
@WebServlet("/course")
@MultipartConfig
public class CourseServlet extends BaseServlet {

    ObjectMapper mapper = new ObjectMapper();

    CourseService courseService = new CourseServiceImpl();

    ResultVO resultVO = null;

    /**
     * 上传文件
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void uploadFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String fileName = FileUploadUtils.fileUpload(request);

        try {
            if (fileName.contains(".jpg")||fileName.contains(".png")||fileName.contains(".jpeg")||
                    fileName.contains(".bmp")||fileName.contains(".gif")){
                resultVO = new ResultVO(201,"上传图片成功",fileName);
            }else if(fileName.contains(".avi")||fileName.contains(".mp4")||fileName.contains(".rmvb")){
                resultVO = new ResultVO(202,"上传视频成功",fileName);
            }else {
                resultVO = new ResultVO(500,"上传失败",null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            response.getWriter().print(mapper.writeValueAsString(resultVO));
        }
    }

    /**
     * 移除上传的文件
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void removeFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String fileName = request.getParameter("fileName");
        File file = new File("F:\\IdeaProjects\\中公-JavaWeb\\upload\\",fileName);
        boolean flag = file.delete();
        if (flag){
            resultVO = new ResultVO(200,"移除文件成功！",null);
        }else {
            resultVO = new ResultVO(500,"移除文件失败！",null);
        }
        response.getWriter().print(mapper.writeValueAsString(resultVO));
    }

    /**
     * 添加课程信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public void addCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {

        Course course = new Course();
        BeanUtils.populate(course,request.getParameterMap());
        boolean flag = courseService.addCourse(course);
        if (flag){
            resultVO = new ResultVO(200,"添加课程信息成功",null);
        }else {
            resultVO = new ResultVO(500,"添加课程信息失败",null);
        }

        response.getWriter().print(mapper.writeValueAsString(resultVO));

    }

    /**
     * 分页查询课程信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void pageCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String search = request.getParameter("search");
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        int currentPage = Integer.parseInt(request.getParameter("currentPage"));
        PageUtils<Course> pageUtils = courseService.pageCourse(search,pageSize,currentPage);
        if (pageUtils!=null){
            resultVO = new ResultVO(200,"查询课程信息成功！",pageUtils);
        }else {
            resultVO = new ResultVO(500,"查询课程信息失败！",null);
        }

        response.getWriter().print(mapper.writeValueAsString(resultVO));

    }

    /**
     * 修改课程信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public void updateCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {

        Course course = new Course();
        BeanUtils.populate(course,request.getParameterMap());
        boolean flag = courseService.updateCourse(course);
        if (flag){
            resultVO = new ResultVO(200,"修改课程信息成功",null);
        }else {
            resultVO = new ResultVO(500,"修改课程信息失败",null);
        }

        response.getWriter().print(mapper.writeValueAsString(resultVO));

    }

    /**
     * 批量删除课程信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void batchDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String[] cids = request.getParameterValues("cid");
        boolean flag = courseService.deleteCourse(cids);
        if (flag){
            resultVO = new ResultVO(200,"批量删除成功！",null);
        }else {
            resultVO = new ResultVO(200,"批量删除失败！",null);
        }
        response.getWriter().print(mapper.writeValueAsString(resultVO));
    }

    /**
     * 查询所有的课程信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findAllCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Course> courses = courseService.selectCourseAll();
        if (courses!=null&&courses.size()>0){
            resultVO = new ResultVO(200,"查询成功！",courses);
        }else {
            resultVO = new ResultVO(500,"查询失败！",null);
        }

        response.getWriter().print(mapper.writeValueAsString(resultVO));
    }
}
